<template>
  <div id="import">
    <upload-excel :on-success="onSuccess" />
  </div>
</template>

<script>
import { formatDate_day } from '@/utils'
import { importEmployee } from '@/api/employees.js'
export default {
  name: 'Import',
  data() {
    return {

    }
  },

  // 定义方法
  methods: {
    async onSuccess({ results }) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const excelArr = results.map(item => {
        const obj = {}
        Object.keys(item).forEach(key => {
          if (key === '入职日期' || key === '转正日期') {
            obj[userRelations[key]] = formatDate_day(item[key], '/')
            return false
          }
          obj[userRelations[key]] = item[key]
        })
        return obj
      })
      await importEmployee(excelArr)
      this.$message.success('添加成功！')
      this.$router.push('/employees')
    }
  }

}
</script>

<style scoped lang='less'>

</style>
